package com.example.himalayaproject.find.broadcast.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.broadcast.activity.GridViewActivity;
import com.example.himalayaproject.find.broadcast.bean.Categories;

import java.util.List;

/**
 * Created by Administrator on 2016/12/6.
 */

public class GridAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private LayoutInflater inflater;
    private List<Categories> categories;
    private List<Categories> data;
    private OnImageViewClickListener listener;
    private String categoryId;
    private String categoryName;

    public GridAdapter(Context context,List<Categories> categories,List<Categories> data,OnImageViewClickListener listener) {
        this.context = context;
        this.categories = categories;
        this.data = data;
        this.listener = listener;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return categories.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;{
            if (position != categories.size()-1){
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.bct_grid_item,parent,false);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();

                }
                categoryId = categories.get(position).getId();
                categoryName = categories.get(position).getName();
                holder.nameTextView.setText(categories.get(position).getName());
//                holder.nameTextView.setOnClickListener(this);
                holder.gridItemLayout.setOnClickListener(this);
                return convertView;
            }else {
                if (categories.size()==8){
                    convertView = inflater.inflate(R.layout.bct_grid_show,parent,false);
                    holder = new ViewHolder(convertView);
                    holder.showImageView.setOnClickListener(this);
                    return convertView;
                }else if (categories.size()==16){
                    convertView = inflater.inflate(R.layout.bct_grid_hide,parent,false);
                    holder = new ViewHolder(convertView);
                    holder.hideImageView.setOnClickListener(this);
                    return convertView;
                }else {
                    return convertView;
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bct_iv_show:
                categories.clear();
                categories.addAll(data);
                categories.add(data.get(0));
                listener.onImageViewClick(categories);
                break;
            case R.id.bct_iv_hide:
                categories.clear();
                for (int i = 0; i < 8; i++) {
                    categories.add(i,data.get(i));
                }
                listener.onImageViewClick(categories);
                break;
            case R.id.grid_item_layout:
                Intent intent = new Intent(context, GridViewActivity.class);
                intent.putExtra("categoryId",categoryId);
                intent.putExtra("categoryName",categoryName);
                this.context.startActivity(intent);
                break;
        }
    }


    private static class ViewHolder {
        TextView nameTextView;
        ImageView showImageView,hideImageView;
        LinearLayout gridItemLayout;

        public ViewHolder(View convertView) {
            nameTextView = ((TextView) convertView.findViewById(R.id.bct_grid_item_name));
            showImageView = ((ImageView) convertView.findViewById(R.id.bct_iv_show));
            hideImageView = ((ImageView) convertView.findViewById(R.id.bct_iv_hide));
            gridItemLayout = ((LinearLayout) convertView.findViewById(R.id.grid_item_layout));
        }
    }

    public interface OnImageViewClickListener {
        void onImageViewClick (List<Categories> list) ;
    }
}
